package com.ys.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.WebConstants;
import com.ys.extend.common.model.AjaxJson;
import com.ys.web.pojo.BackupInfo;
import com.ys.web.pojo.CompanyHasBackup;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.pojo.SpecialCompanyInfo;
import com.ys.web.pojo.SystemPermissonInfo;
import com.ys.web.security.SystemPermissionUtils;
import com.ys.web.service.CompanyService;
import com.ys.web.service.SpecialCompanyService;

@Scope("prototype")
@Controller
@RequestMapping("/specialCompanyController")
public class SpecialCompanyController {

	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private SpecialCompanyService specialCompanyService;

	@RequestMapping(params = "showSpecialCompany")
	public ModelAndView specialCompanyIndex(
			HttpServletRequest httpServletRequest) {
		return new ModelAndView("company/specialCompany");
	}

	@RequestMapping(params = "getCompanys")
	@ResponseBody
	public List<SpecialCompanyInfo> getCompanys(Integer page,
			HttpSession session) {
		String username = (String) session.getAttribute("username");
		List<SpecialCompanyInfo> specialCompanyList = new ArrayList();
		Integer userId = (Integer) session.getAttribute("userId");
		List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
		if (username != null && userId != null) {

			SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils
					.getUserPermission(username);
			Integer infopage = 1;
			if (page != null) {
				infopage = page;
			}

			try {
				if (systemPermissonInfo.getRoleId().contains(
						WebConstants.SUPER_USER)
						|| systemPermissonInfo.getRoleId().contains(
								WebConstants.SEM_MANAGER_USER)) {
					companyInfos = companyService
							.getRemoteCompanyBySemUserId_baidu(null, infopage);
				} else {
					companyInfos = companyService
							.getRemoteCompanyBySemUserId_baidu(userId, infopage);
				}

				
				for (CompanyInfo companyInfo : companyInfos) {
					SpecialCompanyInfo specialCompanyInfo = new SpecialCompanyInfo();
					specialCompanyInfo.setCompanyId(companyInfo.getId());
					specialCompanyInfo.setCompanyName(companyInfo.getCompanyName());
					specialCompanyInfo.setIndustryName(companyInfo.getIndustry());
					String companyLabel = specialCompanyService.getSpecialCompanyById(companyInfo.getId());
					if(companyLabel!=null){
						specialCompanyInfo.setCompanyLabel(companyLabel);
					}else{
						specialCompanyInfo.setCompanyLabel("普通消费用户");
					}
					specialCompanyList.add(specialCompanyInfo);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return specialCompanyList;
		}
		return null;
	}
	
	@RequestMapping(params = "signSpecial")
	@ResponseBody
	public String signSpecial(HttpServletRequest httpServletRequest) {
		int companyId = Integer.valueOf(httpServletRequest.getParameter("companyId"));
		String companyName = httpServletRequest.getParameter("companyName");
		String industryName = httpServletRequest.getParameter("industryName");
		return specialCompanyService.addSpecialCompany(companyId, companyName, industryName);
	}
	@RequestMapping(params = "signGeneral")
	@ResponseBody
	public String signGeneral(HttpServletRequest httpServletRequest) {
		int companyId = Integer.valueOf(httpServletRequest.getParameter("companyId"));
		
		return specialCompanyService.removeSpecialCompany(companyId);
	}

}
